package top.vains.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 跨域处理类
 *
 * @author vains
 * @version 1.0
 * @since 2020/2/18 15:29
 **/
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Value("${file.path}")
    private String basePath;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8081", "https://file.disk.vains.top", "http://localhost:8082", "http://vains.easy.echosite.cn")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true);
    }

    /**
     * 注册静态文件的自定义映射路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //定义到硬盘
        registry.addResourceHandler("/file/**")
                .addResourceLocations("file:" + basePath);
    }

}
